import educationList from '../config/education'

// 获取学生列表
let getStudentList = async (ctx, next) => {
  let studentData = await ctx.db.querys(`select * from student`)
  let userData = await ctx.db.querys(`select * from user`)
  let classData = await ctx.db.querys(`select * from class`)

  let obj = {};
  studentData.forEach(item => {
    if (!obj[item.school]) {
      obj[item.school] = 1
    } else {
      obj[item.school]++
    }
  })
  ctx.body = { code: 1, msg: '学生列表数据获取成功', data: { studentData, userData, classData, obj } }
}

// 获取学历列表
let getEducationList = (ctx, next) => {
  ctx.body = { code: 1, msg: '学历列表数据获取成功', data: educationList }
}


// 学生列表模糊搜索
let searchStudent = async (ctx, next) => {
  const { name, school, course, education } = ctx.request.body;
  let studentData = await ctx.db.querys(`select * from student`);
  let classData = await ctx.db.querys(`select * from class`);
  if (!studentData.length) {
    return ctx.body = { code: 0, msg: '暂无数据', data: [] }
  }

  if (name !== '') {
    studentData = studentData.filter(item => item.name.indexOf(name) !== -1)
  }
  if (school !== 'all') {
    studentData = studentData.filter(item => item.school === school)
  }
  if (course !== 'all') {
    classData = classData.filter(item => item.course == course)
    let index = [];
    classData.forEach(item => {
      index.push(item.classid)
    })
    let arr = [];
    index.forEach(item => {
      let a = studentData.filter(jtem => jtem.classid === item * 1)
      arr = arr.concat(a)
    })
    studentData = arr
  }
  if (education !== 'all') {
    studentData = studentData.filter(item => item.education === education)

  }
  ctx.body = { code: 1, msg: '数据请求成功', data: studentData }
}


// 学生列表点击编辑 获取数据 形成默认值
let getSetStundent = async (ctx, next) => {
  let isOpr = await ctx.isJudge(ctx, 'E');
  if (!isOpr) {
    ctx.body = { code: 0, msg: '您无权操作' }
    return
  }
  const { studentid } = ctx.request.body;
  let studenteData = await ctx.db.querys(`select * from student where studentid='${studentid}'`)
  ctx.body = { code: 1, msg: '数据请求成功', data: studenteData }
}


// 编辑之后确定,修改数据库数据
let setStudnet = async (ctx, next) => {
  const { studentid, sex, education, school, age, classid } = ctx.request.body;
  let studentData = await ctx.db.querys(`select * from student where studentid='${studentid}'`)
  if (!studentData.length) {
    return ctx.body = { code: 0, msg: '未找到该数据' }
  }

  let dbReturn = await ctx.db.querys(`update student set sex='${sex}',age='${age}',education='${education}',school='${school}',classid='${classid}' where studentid='${studentid}'`)

  if (!dbReturn.affectedRows) {
    ctx.body = { code: 0, msg: '操作失败,请重新再来' }
    return
  }
  ctx.body = { code: 1, msg: '数据请求成功' }
}


// 添加学生列表
let addStudent = async (ctx, next) => {

  let isOpr = await ctx.isJudge(ctx, 'A');

  // 判断当前用户是否可以操作
  if (!isOpr) {
    ctx.body = { code: 0, msg: '您无权操作' }
    return
  }
  // 如果权限够  就可以添加
  const { name, age, sex, education, classid, setid } = ctx.request.body;

  //  给学生添加座位号
  let setList = await ctx.db.querys(`select studentList from setList where classid='${classid}'`)
  let a = setList[0].studentList + ',' + setid


  await ctx.db.querys(`update setList set studentList='${a}' where classid=${classid}`)
  let createTime = new Date().getTime()
  let Data = await ctx.db.querys(`select coachid,school from class where classid='${classid}'`)
  let coachid = Data[0].coachid
  let school = Data[0].school
  let results = await ctx.db.querys(`insert into student (name,age,sex,education,classid,coachid,createTime,school) values ('${name}','${age}','${sex}','${education}','${classid}','${coachid}','${createTime}','${school}')`)

  if (results.affectedRows === 1) {
    ctx.body = { code: 1, msg: '添加成功', data: a }
    return
  }
  ctx.body = { code: 0, msg: '添加失败' }

}

//  获取所有教师数据
let getTeacherList = async (ctx, next) => {
  let roleid = await ctx.db.querys(`select roleid from role where roleName='讲师'`)
  let teacherList = await ctx.db.querys(`select * from user where roleid='${roleid[0].roleid}'`)
  ctx.body = { code: 1, msg: '老师数据请求成功', data: teacherList }
}


//  获取所有辅导员数据
let getCoachList = async (ctx, next) => {
  let roleid = await ctx.db.querys(`select roleid from role where roleName='辅导员'`)
  let coachList = await ctx.db.querys(`select * from user where roleid='${roleid[0].roleid}'`)
  ctx.body = { code: 1, msg: '辅导员数据请求成功', data: coachList }
}


//  座位表渲染
let getSetList = async (ctx, next) => {
  const { classid } = ctx.request.body;
  let setList = await ctx.db.querys(`select studentList from setList where classid='${classid}'`);
  ctx.body = { code: 1, msg: '座位表获取成功', data: setList }
}



export default {
  getStudentList, getEducationList, searchStudent, getSetStundent, setStudnet, addStudent, getTeacherList, getCoachList, getSetList
}

